package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.SjBidding;
import com.ruoyi.gcgl.mapper.SjBiddingMapper;
import com.ruoyi.gcgl.service.ISjBiddingService;

/**
 * 投标管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@Service
public class SjBiddingServiceImpl implements ISjBiddingService 
{
    @Autowired
    private SjBiddingMapper sjBiddingMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询投标管理
     * 
     * @param id 投标管理主键
     * @return 投标管理
     */
    @Override
    public SjBidding selectSjBiddingById(Long id)
    {
        return sjBiddingMapper.selectSjBiddingById(id);
    }

    /**
     * 查询投标管理列表
     * 
     * @param sjBidding 投标管理
     * @return 投标管理
     */
    @Override
    public List<SjBidding> selectSjBiddingList(SjBidding sjBidding)
    {
        return sjBiddingMapper.selectSjBiddingList(sjBidding);
    }

    /**
     * 新增投标管理
     * 
     * @param sjBidding 投标管理
     * @return 结果
     */
    @Override
    public int insertSjBidding(SjBidding sjBidding)
    {
    	int rows = sjBiddingMapper.insertSjBidding(sjBidding);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(sjBidding.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", sjBidding.getOwner());
        variables.put("level1", sjBidding.getSfbzdj());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjtoubiao", String.valueOf(sjBidding.getId()), variables);
    	System.out.println("-------"+pi);
    	
    	SjBidding obj = new SjBidding();
    	obj.setId(sjBidding.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	sjBiddingMapper.updateSjBidding(obj);
    	
        return rows;
    }

    /**
     * 修改投标管理
     * 
     * @param sjBidding 投标管理
     * @return 结果
     */
    @Override
    public int updateSjBidding(SjBidding sjBidding)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(sjBidding.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", sjBidding.getOwner());
//        variables.put("level1", sjBidding.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjtoubiao", String.valueOf(sjBidding.getId()), variables);
//        sjBidding.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return sjBiddingMapper.updateSjBidding(sjBidding);
    }
    
    @Override
    public int updateSjBidding4Check(SjBidding sjBidding)
    {
        return sjBiddingMapper.updateSjBidding(sjBidding);
    }

    /**
     * 批量删除投标管理
     * 
     * @param ids 需要删除的投标管理主键
     * @return 结果
     */
    @Override
    public int deleteSjBiddingByIds(String ids)
    {
        return sjBiddingMapper.deleteSjBiddingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除投标管理信息
     * 
     * @param id 投标管理主键
     * @return 结果
     */
    @Override
    public int deleteSjBiddingById(Long id)
    {
        return sjBiddingMapper.deleteSjBiddingById(id);
    }
}
